<template>
  <a-layout class="layout">
    <a-layout-sider v-model:collapsed="collapsed" :trigger="null" collapsible>
      <div class="logo"> {{$t('Survey')}}</div>
      <a-menu theme="dark" mode="inline" v-if="userRole === 'front'" v-model:selectedKeys="selectedKeys" @select="selectMenu">
        <a-menu-item key="myAccount">
          <user-outlined />
          <span> {{$t('My Points')}}</span>
        </a-menu-item>
        <a-menu-item key="surveyList">
          <HddOutlined />
          <span> {{$t('Survey List')}}</span>
        </a-menu-item>
        <a-menu-item key="mySurvey">
          <SelectOutlined />
          <span> {{$t('My Survey')}}</span>
        </a-menu-item>
      </a-menu>
      <a-menu theme="dark" mode="inline" v-if="userRole === 'admin'" v-model:selectedKeys="selectedKeys" @select="selectMenu">
        <a-menu-item key="surveyPublish">
          <user-outlined />
          <span> {{$t('Survey Publish')}}</span>
        </a-menu-item>
        <a-menu-item key="surveyReview">
          <HddOutlined />
          <span> {{$t('Survey Review')}}</span>
        </a-menu-item>
        <a-menu-item key="surveyStatistics">
          <SelectOutlined />
          <span> {{$t('Survey Analysis')}}</span>
        </a-menu-item>
      </a-menu>
    </a-layout-sider>
    <a-layout>
      <a-layout-header style="background: #fff; padding: 0;display:flex;justify-content: space-between">
        <menu-unfold-outlined v-if="collapsed" class="trigger" @click="() => (collapsed = !collapsed)" />
        <menu-fold-outlined v-else class="trigger" @click="() => (collapsed = !collapsed)" />
        <a-space class="user-info">
            <!-- <span>{{userRole}}</span> -->
            <LogoutOutlined  @click="loginOut"/>
        </a-space>
      </a-layout-header>
      <a-layout-content :style="{ margin: '24px 16px', padding: '24px', background: '#fff', minHeight: '280px' }">
       <router-view></router-view>
      </a-layout-content>
    </a-layout>
  </a-layout>
</template>
<script lang="ts">
import {
  UserOutlined,
  VideoCameraOutlined,
  UploadOutlined,
  MenuUnfoldOutlined,
  MenuFoldOutlined,
  SelectOutlined,
  HddOutlined,
  LogoutOutlined
} from "@ant-design/icons-vue";
import { defineComponent, ref, watch } from "vue";
import { useRouter, useRoute } from "vue-router"
import { handleLogout } from "../../api/survey"
import { setCookie } from "../../utils/common";
export default defineComponent({
  components: {
    UserOutlined,
    VideoCameraOutlined,
    UploadOutlined,
    MenuUnfoldOutlined,
    MenuFoldOutlined,
    SelectOutlined,
    HddOutlined,
    LogoutOutlined
  },
  setup() {
    const role = window.localStorage.getItem('user_type');
    const userRole = ref(role)
    const router = useRouter()
    const selectMenu = (item) => {
        router.push({path: '/' + item.key})
    }
    const route = useRoute()
    const selectedKeys = ref<string[]>([route.name])
    const loginOut = () => {
        handleLogout().then(res => {
          setCookie("survey_user_token",'',0);
          window.localStorage.clear()
          router.push({path: '/'})
        })
        
    }
    watch(route, (v,n) => {
      selectedKeys.value = [n.name]
    })
    return {
      selectedKeys,
      collapsed: ref<boolean>(false),
      userRole: userRole,
      selectMenu,
      loginOut
    };
  },
});
</script>
<style scoped>

.layout {
  height: 100%;
}
.trigger {
  font-size: 18px;
  line-height: 64px;
  padding: 0 24px;
  cursor: pointer;
  transition: color 0.3s;
}

.trigger:hover {
  color: #1890ff;
}

 .logo {
  margin: 16px;
  font-size: 32px;
  color: #fff;
}

.site-layout .site-layout-background {
  background: #fff;
}
.user-info{
    padding-right: 20px;
    font-weight: bold;
    font-size: 16px;
    
}
</style>
